<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row>
        <el-button type="primary" @click="getParentCateList">添加分类</el-button>
      </el-row>

      <!-- 表格 -->
      <!-- 使用第三方的树形表格 -->
      <zk-table
        :data="goodsCateList"
        :columns="columns"
        show-index
        stripe
        border
        :selection-type="false"
        :expand-type="false"
      >
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-circle-check" v-if="scope.row.cat_deleted==false" style="color:green"></i>
          <i class="el-icon-circle-close" v-else style="color:red"></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level==0">一级标签</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level==1">二级标签</el-tag>
          <el-tag type="warning" v-else>三级标签</el-tag>
        </template>
        <template slot="opt" slot-scope="scope">
          <el-button type="primary" size="mini" class="el-icon-edit">编辑</el-button>
          <el-button type="danger" size="mini" class="el-icon-delete">删除</el-button>
        </template>
      </zk-table>

      <!-- 分页区域 -->

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>

      <!-- 点击添加分类的弹框 -->

      <el-dialog title="添加分类" :visible.sync="addCatedialogVisible" width="50%" @close="addCateDialogClose">
        <el-form
          ref="addCateFormRef"
          :model="addCateForm"
          label-width="80px"
          :rules="addCateFormRules"
        >
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="addCateForm.cat_name"></el-input>
          </el-form-item>

          <!-- 分类级联选择器 -->
          <el-form-item label="父级分类">
            <el-cascader
              v-model="selectedOptions"
              :options="ParentCateList"
              :props="cascaderProps"
              @change="pargentCateChange"
              clearable
              
            ></el-cascader>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="addCatedialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCate">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
           
<script>
export default {
  data() {
    return {
      // 查询参数
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      // 获取到的商品的分类数据
      goodsCateList: [],
      // 指定每列的数据
      columns: [
        { label: '分类名称', prop: 'cat_name' },
        {
          label: '是否有效',
          //  定义为模板列
          type: 'template',
          //   模板的名字
          template: 'isok',
          align: 'center'
        },
        {
          label: '排序',
          //  定义为模板列
          type: 'template',
          //   模板的名字
          template: 'order',
          align: 'center'
        },
        {
          label: '排序',
          //  定义为模板列
          type: 'template',
          //   模板的名字
          template: 'opt'
        }
      ],
      ParentCateList: [],
      addCatedialogVisible: false,
      selectedOptions: [],
      cascaderProps: {
        label: 'cat_name',
        children: 'children',
        value: 'cat_id',
        expandTrigger:"hover",
        checkStrictly:true,


      },
      addCateForm: {
        cat_name: '',
        //   定义父分类的id
        cat_pid: 0,
        //   定义父分类的等级
        cat_level: 0,
        
      },
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    async getgoodsList() {
      let { data: res } = await this.axios.get('categories', {
        params: this.queryInfo
      });
      //    console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品数据失败');
      }
      (this.queryInfo.pagenum = res.data.pagenum),
        (this.queryInfo.pagesize = res.data.pagesize),
        (this.total = res.data.total);
      this.goodsCateList = res.data.result;
      //   console.log(this.goodsCateList);
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.queryInfo.pagesize = val;
      this.getgoodsList();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.queryInfo.pagenum = val;
      this.getgoodsList();
    },
    async getParentCateList() {
      let { data: res } = await this.axios.get('categories', {
        params: { type: 2 }
      });
      //   console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error('请求出错');
      }
      this.ParentCateList = res.data;
      this.addCatedialogVisible = true;
    },
    // 选择触动的事件
    pargentCateChange() {
    //   console.log(this.selectedOptions,999);
    if(this.selectedOptions.length>0){
        // 获取数组上级父节点的id
        this.addCateForm.cat_pid=this.selectedOptions[this.selectedOptions.length-1];
        this.addCateForm.cat_level=this.selectedOptions.length
          console.log(this.selectedOptions);
    }else {
        this.addCateForm.cat_pid=0;
        this.addCateForm.cat_pid=0;
    }

    },
    // 点击确定
    async addCate() {
    //   console.log(this.addCateForm);
    // 发送请求添加新的分类
    let {data:res}=await this.axios.post('categories',this.addCateForm)
    console.log(res);
    if(res.meta.status!==201) {
        return this.$message.error('添加分类失败')
    }
    this.$message.success('添加分类成功')
    this.getgoodsList();
    
      
      this.addCatedialogVisible = false;
    },
    addCateDialogClose(){
        // console.log(123);
        // console.log(this.$refs);
        
        this.$refs.addCateFormRef.resetFields();
        this.addCateForm.cat_pid=0;
        this.addCateForm.cat_level=0;
        // 重置选择到的借点信息为空
        this.selectedOptions=[]
        
    }
  },
  mounted() {
    this.getgoodsList();
  }
};
</script>
<style scoped lang="less">
.el-card {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>